﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using ColorApplication.Helper;
using System.Drawing;
using System.Configuration;

namespace ColorApplication.color
{
    public partial class SubColors : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Checking whether Admin or Not. AdminID key is in web.config
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "" && Request.QueryString["id"] == ConfigurationManager.AppSettings["AdminID"].ToString())
            {
                string s = Request.QueryString["id"];
                Clear();
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
            try
            {

                // If ColorId is Pass Set value for Data Source of grid view
                if (Request.QueryString["ColorId"] != null)
                {
                    if (Request.QueryString["ColorId"].ToString() != string.Empty)
                    {
                        int ColorId = Convert.ToInt32(Request.QueryString["ColorId"].ToString());
                        SubColorGridSqlDataSource.SelectParameters.Clear();

                        Parameter parameter1 = new Parameter("ColorId", DbType.Int32, ColorId.ToString());
                        SubColorGridSqlDataSource.SelectParameters.Add(parameter1);

                        ControlParameter parameter2 = new ControlParameter("Description", DbType.String, "SearchDescriptionTextBox", "Text");
                        SubColorGridSqlDataSource.SelectParameters.Add(parameter2);

                    }
                }
                else
                {
                    // If Next Time remove ColorId value for Data Source of grid view
                    SubColorGridSqlDataSource.SelectParameters.Clear();

                    Parameter parameter1 = new Parameter("ColorId");
                    SubColorGridSqlDataSource.SelectParameters.Add(parameter1);


                    ControlParameter parameter2 = new ControlParameter("Description", DbType.String, "SearchDescriptionTextBox", "Text");
                    SubColorGridSqlDataSource.SelectParameters.Add(parameter2);
                }

            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }
        public void Clear()
        {
            // Clear Error And Success label Every Post Bak
            SuccesLabel.Text = "";
            ErrorLabel.Text = "";

        }

        protected void SubColorGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                // Change Deatils view Mode to edit
                SubColorDetailsView.ChangeMode(DetailsViewMode.Edit);
                FindRGBSHow();
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void SubColorDetailsView_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            try
            {
                // Show Success Message 
                // Grid data Binding
                SuccesLabel.Text = "Record updated successfully.";
                SubColorDetailsView.ChangeMode(DetailsViewMode.Insert);
                SubColorGridView.DataBind();
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void SubColorDetailsView_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            try
            {
                // Show Success Message 
                // Grid data Binding
                SuccesLabel.Text = "Record inserted successfully.";

                SubColorDetailsView.ChangeMode(DetailsViewMode.Insert);
                SubColorGridView.DataBind();

            }
            catch (Exception err)
            {


                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void SubColorGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                // Bind java Script for Are you Sure to Delete Confirm Box
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    LinkButton delinkbutton = (LinkButton)e.Row.FindControl("DeleteLinkButton");
                    delinkbutton.OnClientClick = "return getDeleteMessage()";


                    // In Grid view Show Color Caccording to RGB value
                    int Rvalue = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "RCode"));
                    int Gvalue = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "GCode"));
                    int Bvalue = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "BCode"));
                    TextBox ShowColorTextBox = (TextBox)e.Row.FindControl("ShowColorTextBox");
                    ShowColorTextBox.BackColor = ShowColor(Rvalue, Gvalue, Bvalue);

                }
            }
            catch (Exception err)
            {

                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void SubColorDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            try
            {
                string oldDescription = e.OldValues["Description"].ToString();
                string newDescription = e.NewValues["Description"].ToString();

                if (oldDescription != newDescription)
                {
                    // This Function Check Record in Sub Color for Dublicate Description exist or Not
                    if (CheckDescriptionExistinSubcolors(newDescription) == true)
                    {
                        ErrorLabel.Text = "Description already exists. Duplicate record is not allowed.";
                        e.Cancel = true;
                        //Cancel And Show Error Message
                    }

                }

            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void SubColorDetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            try
            {
                string Description = e.Values["Description"].ToString();

                // This Function Check Record in Sub Color for Dublicate Description exist or Not
                if (CheckDescriptionExistinSubcolors(Description) == true)
                {
                    ErrorLabel.Text = "Description already exists. Duplicate record is not allowed.";
                    e.Cancel = true;
                    //Cancel And Show Error Message
                }


            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        public bool CheckDescriptionExistinSubcolors(string Description)
        {
            // This Function Check Record in Sub Color Table For Dublicate Description Exist or Not
            // if Record Found Return true
            //else return false

            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection())
            {
                SqlParameter[] parameters = { new SqlParameter("@Description", Description) };

                SqlCommand cmd = SQLHelper.GetCommand(con, "CheckDescriptionExistinSubcolors", parameters);
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                return true;
            }

            return false;

        }




        /// <summary>
        /// this method accept r,g,b parameter and show respective color in details view
        /// </summary>
        /// <param name="r"></param>
        /// <param name="g"></param>
        /// <param name="b"></param>
        public Color ShowColor(int r, int g, int b)
        {
            // This Method Show Color in olor
            Color color = Color.FromArgb(r, g, b);
            return color;
        }
        /// <summary>
        /// this method accept parameter textBox and return in that value
        /// </summary>
        /// <param name="textbox"></param>
        /// <returns></returns>
        public int GetTextBoxValue(TextBox textbox)
        {
            if (textbox != null)
            {
                if (textbox.Text != string.Empty)
                {
                    int val = Convert.ToInt32(textbox.Text);
                    return val;
                }
                else
                {
                    return 0;

                }

            }

            return 0;
        }
        public void FindRGBSHow()
        {
            // This Method Show Color in textBox Back Ground Color
            if (SubColorDetailsView.CurrentMode == DetailsViewMode.Insert)
            {
                // Find RBG Value from text Box And Show Value in TextBox Back ground Value
                TextBox InsertRCodeTextBox = (TextBox)SubColorDetailsView.FindControl("InsertRCodeTextBox");
                TextBox InsertGcodeTextBox = (TextBox)SubColorDetailsView.FindControl("InsertGcodeTextBox");
                TextBox InsertBCodeTextBox = (TextBox)SubColorDetailsView.FindControl("InsertBCodeTextBox");
                TextBox InsertShowColorTextBox = (TextBox)SubColorDetailsView.FindControl("InsertShowColorTextBox");

                int Rvalue = GetTextBoxValue(InsertRCodeTextBox);
                int Gvalue = GetTextBoxValue(InsertGcodeTextBox);
                int Bvalue = GetTextBoxValue(InsertBCodeTextBox);

                // set Default value 0
                if (InsertRCodeTextBox != null && Rvalue == 0)
                {
                    InsertRCodeTextBox.Text = "0";
                }
                if (InsertGcodeTextBox != null && Gvalue == 0)
                {
                    InsertGcodeTextBox.Text = "0";
                }
                if (InsertBCodeTextBox != null && Bvalue == 0)
                {
                    InsertBCodeTextBox.Text = "0";
                }

                if (InsertShowColorTextBox != null)
                {
                    InsertShowColorTextBox.BackColor = ShowColor(Rvalue, Gvalue, Bvalue);
                }


            }

            if (SubColorDetailsView.CurrentMode == DetailsViewMode.Edit)
            {
                // Find RBG Value from text Box And Show Value in TextBox Back ground Value
                TextBox EditRCodeTextBox = (TextBox)SubColorDetailsView.FindControl("EditRCodeTextBox");
                TextBox EditGcodeTextBox = (TextBox)SubColorDetailsView.FindControl("EditGcodeTextBox");
                TextBox EditBCodeTextBox = (TextBox)SubColorDetailsView.FindControl("EditBCodeTextBox");
                TextBox EditShowColorTextBox = (TextBox)SubColorDetailsView.FindControl("EditShowColorTextBox");
                int Rvalue = GetTextBoxValue(EditRCodeTextBox);
                int Gvalue = GetTextBoxValue(EditGcodeTextBox);
                int Bvalue = GetTextBoxValue(EditBCodeTextBox);
                if (EditShowColorTextBox != null)
                {
                    EditShowColorTextBox.BackColor = ShowColor(Rvalue, Gvalue, Bvalue);
                }
            }

        }

        protected void InsertRCodeTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                // Set Color R
                FindRGBSHow();
                // Set Focus to next Conrol
                TextBox InsertGcodeTextBox = (TextBox)SubColorDetailsView.FindControl("InsertGcodeTextBox");
                if (InsertGcodeTextBox != null)
                {
                    InsertGcodeTextBox.Focus();

                }
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();

            }
        }
        protected void InsertGcodeTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                // Set Color G
                FindRGBSHow();
                // Set Focus to next Conrol
                TextBox InsertBCodeTextBox = (TextBox)SubColorDetailsView.FindControl("InsertBCodeTextBox");
                if (InsertBCodeTextBox != null)
                {
                    InsertBCodeTextBox.Focus();

                }
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();

            }
        }
        protected void InsertBCodeTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                // Set Color B
                FindRGBSHow();

                // Set Focus to next Conrol
                LinkButton InsertLinkButton = (LinkButton)SubColorDetailsView.FindControl("InsertLinkButton");
                if (InsertLinkButton != null)
                {
                    InsertLinkButton.Focus();

                }
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();

            }
        }
        protected void EditRCodeTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                // Set Color R
                FindRGBSHow();
                // Set Focus to next Conrol
                TextBox EditGcodeTextBox = (TextBox)SubColorDetailsView.FindControl("EditGcodeTextBox");
                if (EditGcodeTextBox != null)
                {
                    EditGcodeTextBox.Focus();

                }

            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();

            }
        }
        protected void EditGcodeTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                // Set Color G
                FindRGBSHow();
                // Set Focus to next Conrol
                TextBox EditBCodeTextBox = (TextBox)SubColorDetailsView.FindControl("EditBCodeTextBox");
                if (EditBCodeTextBox != null)
                {
                    EditBCodeTextBox.Focus();

                }

            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();

            }
        }
        protected void EditBCodeTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                // Set Color B
                FindRGBSHow();

                // Set Focus to next Conrol
                LinkButton UpdateLinkButton = (LinkButton)SubColorDetailsView.FindControl("UpdateLinkButton");
                if (UpdateLinkButton != null)
                {
                    UpdateLinkButton.Focus();

                }
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();

            }
        }

        protected void SubColorDetailsView_DataBound(object sender, EventArgs e)
        {
            try
            {
                // Set Color for Insert And Update
                // Event is fired
                FindRGBSHow();
            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();
            }
        }

        protected void InsertColorDropDownList_DataBound(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["ColorId"] != null)
                {
                    if (Request.QueryString["ColorId"].ToString() != string.Empty)
                    {
                        int ColorId = Convert.ToInt32(Request.QueryString["ColorId"].ToString());
                        DropDownList InsertColorDropDownList = (DropDownList)SubColorDetailsView.FindControl("InsertColorDropDownList");
                        if (InsertColorDropDownList != null)
                        {
                            InsertColorDropDownList.SelectedValue = ColorId.ToString();
                        }
                    }

                }


            }
            catch (Exception err)
            {
                ErrorLabel.Text = err.Message.ToString();

            }
        }
    }
}